package com.excuse.excusebuilder.base;

import android.content.Context;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.WindowManager;


import com.excuse.excusebuilder.receiver.InternetConnectStatusReceiver;

import butterknife.ButterKnife;

/**
 * Created by U-nookia on 2017/4/11.
 * activity基类
 */

public abstract class BaseActivity extends FragmentActivity{

    public Context context = this;
    public final String TAG = this.getClass().getName();
    private InternetConnectStatusReceiver receiver;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResId());
        ButterKnife.bind(this);
        initData();
        initState();  //实现沉浸式状态栏
        checkInternet();  //第一次检查网络
        initInternetStateReceiver();  //注册监听网络状态的广播接收器
    }

    private void checkInternet() {

    }

    private void initInternetStateReceiver() {
        receiver = new InternetConnectStatusReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(receiver,filter);
    }

    private void initState() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    protected abstract void initData();

    public abstract int getLayoutResId();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }
}
